﻿ $(document).ready(function () {
    //if question is empty, focus on submitt button
    $("#TextBoxQuestion").blur(function (event){
        if($(this).val() == ""){
            
            $("#submitButton").focus();
        }
    });

    //create new answer or new questions (if previous answer is empty)
    $("#TextBoxAnswer").blur(function (event) {
    rod = $(this).parents('div:first');
    removeAnswerButton = $(rod.siblings("span").children("a#removeAnswer"));
    
    if ($(this).val() == "" ) {
        $(removeAnswerButton).click(); //remove answer

        var liQuestion = $(this).parents("*[id^=Question]:last");
        if(IsCurrentlyLastQuestion(liQuestion) && QuestionIsEmpty(liQuestion) && FirstAnswerIsEmpty(liQuestion))
        {      
            $("#addQuestion").click();
        }
                                
        var currentQuestion = liQuestion.next($(this).tagName).find("input[id^=TextBoxQuestion]")
                                
        $(currentQuestion).focus();
    }
        });

    function IsCurrentlyLastQuestion(question)
    {
    return $(question).next("li[id^=Question]").length == 0 ? true : false;
    };

    function QuestionIsEmpty(question){
    return true;
    }
    function FirstAnswerIsEmpty(question){
    if($(question).find("input#TextBoxAnswer").length == 0 && $(question).find("input[id^=TextBoxAnswer]").attr("value") == "")
    return false;
    else return true;
    }


    $("#TextBoxAnswer").focus(function (event) {

        rod = $(this).parents('div:first');

        addAnswerButton = $(rod.siblings("span").children("a#addAnswer"));

        if($(rod.next("div").next("div")).length == 0 && rod.next("div").length == 0)
            $(addAnswerButton).click();
        
    });


    //dynamic form settings
    $("#Question").dynamicForm("#addQuestion", "#removeQuestion", {
        createColor: "green" //, removeColor: "black"
    });

    $("#Answer").dynamicForm("#addAnswer", "#removeAnswer", {
        createColor: "green", 
    });

    ///webservice - save data
    $("#submitButton").click(function (event) {
        var questionnaireData = $('#form1').formSerialize();

        $.ajax({
            type: "POST",
            url: "../../WebServices/SaveDataFromQuestionnaire.asmx/SaveData",
            //url: "formusingwebservice.aspx/FunkciaHello2",
            //data: "{'name': '" + $('#name').val() + "'}",
            data: "{'name': '" + questionnaireData + "'}",

            //data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json", //
            success: function (msg) {
                AjaxSucceeded(msg);
            },
            error: AjaxFailed
        });
    });

    function AjaxSucceeded(result) {
    //alert(result.d);
    $("#labelresult").text(result.d);
    alert("hotovo");
}
function AjaxFailed(result) {
    alert(result.status + ' ' + result.statusText);
} 
});